/*******************************************************************************
 * SDP Project AUT 2012
 * 
 *                ***********Driving Assistant************
 * Team:
 * Gisela, Deidre, Brijesh, Robert
 * 
 * This class implements the progress bar used for the light panel to assist in
 * alerting the user that an approaching object is near the specified distance.
 * 
 * @version 1.0
 * 
 * Copyright (c) 2011 by DrivingAssistant Team, Inc., All Rights Reserved.
 * 
 ******************************************************************************/
package com.drivingAssistant.model;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import javax.swing.*;

/********************************
 * Class is still in  test stages
 * *********************************
 * ArduinoLightBar panel class
 * 
 */
public class ArduinoLightBar extends JPanel
                                 implements ActionListener,
                                            PropertyChangeListener {
    // variables 
    private JProgressBar progressBar;
    private JButton startButton;
    private Task task;
 
    /**
     * inner class to set up the threads
     */
    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
            Random random = new Random();
            int progress = 0;
            //Initialize progress property.
            setProgress(0);
            while (progress < 100) {
                //Sleep for up to one second.
                try {
                    Thread.sleep(random.nextInt(1000));
                } catch (InterruptedException ignore) {}
                //Make random progress.
                progress += random.nextInt(10);
                setProgress(Math.min(progress, 100));
            }
            return null;
        }
 
        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            startButton.setEnabled(true);
            setCursor(null); //turn off the wait cursor
//            taskOutput.append("Done!\n");
        }
    }
 
    /**
     * constructor for ArduinoLightBar which initializes class objects
     */
    public ArduinoLightBar() {
        super(new BorderLayout(5,5));
        setSize(200, 380);
        
        progressBar = new JProgressBar(SwingConstants.VERTICAL, 0, 100);
        progressBar.setPreferredSize(new Dimension(200, 350));
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        
        startButton = new JButton("Start");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);
        
 
//        taskOutput = new JTextArea(5, 20);
//        taskOutput.setMargin(new Insets(5,5,5,5));
//        taskOutput.setEditable(false);
 
//        JPanel panel = new JPanel();
        add(progressBar, BorderLayout.NORTH);
        add(startButton, BorderLayout.SOUTH);
//        add(panel, BorderLayout.CENTER);
//        setBorder(BorderFactory.createEmptyBorder(200, 200, 200, 200));
 
    }
 
    /**
     * Invoked when the user presses the start button.
     */
    public void actionPerformed(ActionEvent evt) {
        startButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }
 
    /**
     * Invoked when tasks progress property changes.
     */
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) { // need to check this
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
//            taskOutput.append(String.format(
//                    "Completed %d%% of task.\n", task.getProgress()));
        }
    }
    
}
